package com.langyiquan.front.controller;

import com.langyiquan.bo.ShopcartBO;
import com.langyiquan.bo.UserBO;
import com.langyiquan.bo.WechatLoginBO;
import com.langyiquan.exception.http.ExistException;
import com.langyiquan.exception.http.ParamException;
import com.langyiquan.pojo.Users;
import com.langyiquan.service.SmsService;
import com.langyiquan.service.UserService;
import com.langyiquan.service.WechatService;
import com.langyiquan.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Api(value = "用户注册", tags = {"用户注册登录的相关接口"})
@RestController
@RequestMapping("passport")
public class PassportController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public UnifyResponse usernameIsExist(@RequestParam String username){

        if(StringUtils.isBlank(username)){
            throw new ParamException(1001);
        }

        boolean isExist = userService.queryUsernameIsExist(username);
        if(isExist){
            throw new ExistException(1014);
        }
        return UnifyResponse.success();
    }

    @ApiOperation(value = "登录用户",notes = "登录用户",httpMethod = "POST")
    @PostMapping("/login")
    public UnifyResponse login(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response){
        String username = userBO.getUsername();
        String password = userBO.getPassword();

        if(StringUtils.isBlank(username)){
            throw new ParamException(1001);
        }

        if(StringUtils.isBlank(password)){
            throw new ParamException(1002);
        }

        Users user = userService.queryUserForLogin(username,password);
        if(user == null){
            throw new ParamException(1003);
        }

        String token = JwtHandler.sign(user.getId(), 86400 * 365);
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);

        return UnifyResponse.success(tokenMap);
    }

    @ApiOperation(value = "注册用户",notes = "注册用户",httpMethod = "POST")
    @PostMapping("/register")
    public UnifyResponse register(@RequestBody UserBO userBO,HttpServletRequest request, HttpServletResponse response){

        String username = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPassword = userBO.getConfirmPassword();

        if(StringUtils.isBlank(username)){
            throw new ParamException(1001);
        }

        if(StringUtils.isBlank(password)){
            throw new ParamException(1002);
        }

        if(StringUtils.isBlank(confirmPassword)){
            throw new ParamException(1015);
        }

        boolean isExist = userService.queryUsernameIsExist(username);
        if(isExist){
            throw new ExistException(1014);
        }

        if(!password.equals(confirmPassword)){
            throw new ParamException(1016);
        }

        Users user = userService.createUser(userBO);

        String token = JwtHandler.sign(user.getId(), 86400 * 365);
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);

        return UnifyResponse.success(tokenMap);
    }

    @ApiOperation(value = "发送注册验证码",notes = "发送注册验证码",httpMethod = "POST")
    @PostMapping("/sendSmsCode")
    public UnifyResponse sendSms(@RequestParam String mobile){
        smsService.sendSms(mobile);
        return UnifyResponse.success();
    }

    @ApiOperation(value = "退出登录",notes = "退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public UnifyResponse logout(@RequestParam String userId,HttpServletRequest request, HttpServletResponse response){
        return UnifyResponse.success();
    }

    @ApiOperation(value = "微信小程序登录",notes = "微信小程序登录",httpMethod = "POST")
    @PostMapping("/miniProgramLogin")
    public UnifyResponse miniProgramLogin(@RequestBody WechatLoginBO wechatLoginBO,HttpServletRequest request, HttpServletResponse response){
        Users user = wechatService.getUserInfo(wechatLoginBO);
        String token = JwtHandler.sign(user.getId(), 86400 * 365);
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);

        return UnifyResponse.success(tokenMap);
    }

}
